// 引入标品算价组件
document.write("<script type='text/javascript' src='/statics/js/modules/component/calculateStandPriceComponent.js'></script>");
document.write("<script type='text/javascript' src='/statics/js/modules/component/directives.js'></script>");
var htmlCodes = [
    '   <div id="zyApp" v-cloak>',
    '        <!--菜单展示-->',
    '        <el-tabs type="border-card" @tab-click="changeGoodsTab" v-model="category">',
    '           <el-tab-pane label="快印商品" name="fast">',
    '                   <el-tabs v-model="ziyunCategoryId" type="card" @tab-click="handleFastClick">',
    '                   <el-tab-pane label="快印商品" ',
    '                     v-for="(item, index) in fast"',
    '                      :key="item.categoryId"',
    '                   :label="item.categoryName"',
    '                     :name="item.ziyunCategoryId">',
    '                    </el-tab-pane>',
    '                       </el-tabs>',
    '        <!--产品信息展示-->',
    '        <!--1. 循环部件-->',
    '        <el-collapse v-model="mustChoiceParts" style="padding-left:15px">',
    '            <el-form :inline="true" :model="calculateData" :rules="calculateFormRules"  ref="calculateForm"',
    '                     label-width="100px" class="demo-form-inline" size="mini">',
    '                <el-collapse-item style="color:red"',
    '                        v-for="quoteParts in quoteProductInfo.quotePartsList"',
    '                        :title="quoteParts.name"',
    '                        :name="quoteParts.constructId"',
    '                >',
    '                    <!--展示每个部件quoteAttributeList-->',
    '                    <!--普通属性-->',
    '                    <el-form-item',
    '                            style="margin-bottom: 0px"',
    '                            v-for="quoteAttribute in quoteParts.quoteAttributeList"',
    '                            v-if="quoteAttribute.attributeType == \'normal\' "',
    '                            >',
    '                        <el-form-item',
    '                                style="margin-bottom: 0px"',
    '                                :label="quoteAttribute.name"',
    '                                :prop="\'attributesMap.\'+quoteParts.constructId+\'.\'+quoteAttribute.attributeTypeValue"',
    '                        >',
    '                            <!--展示属性选项-->',
    '                            <el-select',
    '                                    style="width: 130px"',
    '                                    v-model="calculateData.attributesMap[quoteParts.constructId][quoteAttribute.attributeTypeValue]"',
    '                                    clearable placeholder="请选择" >',
    '                                <el-option',
    '                                        v-for="quoteOption in quoteAttribute.quoteAttributeOptionList"',
    '                                        :key="quoteOption.id"',
    '                                        :label="quoteOption.name"',
    '                                        :value="quoteOption.id">',
    '                                </el-option>',
    '                            </el-select>',
    '                        </el-form-item>',
    '                        <!--展示自定义-->',
    '                        <el-checkbox v-model="isShowDefined[quoteParts.constructId][quoteAttribute.attributeTypeValue]"',
    '                                     v-if="quoteAttribute.canDefinition"',
    '                                     @change="defineChange(quoteParts.constructId,quoteAttribute,$event)">',
    '                            自定义',
    '                        </el-checkbox>',
    '                        <el-form-item',
    '                                v-if="isShowDefined[quoteParts.constructId][quoteAttribute.attributeTypeValue]"',
    '                                v-for="(define,index) in definedArr[quoteParts.constructId][quoteAttribute.attributeTypeValue]"',
    '                                :prop="\'definitionValueMap.\'+quoteParts.constructId+\'.\'+quoteAttribute.attributeTypeValue+\'.\'+index">',
    '                            <el-input v-model.number="calculateData.definitionValueMap[quoteParts.constructId][quoteAttribute.attributeTypeValue][index]"',
    '                                      style="width: 70px"',
    '                                      :placeholder="define">',
    '                            </el-input>',
    '                        </el-form-item>',
    '                    </el-form-item>',
    '                    <!--必选显示工艺-->',
    '                    <el-form-item',
    '                            style="margin-bottom: 0px"',
    '                            v-for="quoteAttribute in quoteParts.quoteAttributeList"',
    '                            v-if="quoteAttribute.attributeType == \'craft\' && quoteAttribute.mustChioce == true &&',
    '                            quoteAttribute.attributeDisplay == true "',
    '                            :label="quoteAttribute.name"',
    '                            :prop="\'attributesMap.\'+quoteParts.constructId+\'.\'+quoteAttribute.attributeTypeValue">',
    '                        <!--展示属性选项-->',
    '                        <el-select',
    '                                style="width: 130px"',
    '                                v-model="calculateData.attributesMap[quoteParts.constructId][quoteAttribute.attributeTypeValue]"',
    '                                clearable placeholder="请选择">',
    '                            <el-option',
    '                                    v-for="quoteOption in quoteAttribute.quoteAttributeOptionList"',
    '                                    :key="quoteOption.id"',
    '                                    :label="quoteOption.name"',
    '                                    :value="quoteOption.id">',
    '                            </el-option>',
    '                        </el-select>',
    '                    </el-form-item>',
    '                    <!--材料-->',
    '                    <el-form-item',
    '                            style="margin-bottom: 0px"',
    '                            v-for="quoteAttribute in quoteParts.quoteAttributeList"',
    '                            v-if="quoteAttribute.attributeType == \'material\' "',
    '                            :label="quoteAttribute.name"',
    '                            :prop="quoteParts.constructId+quoteAttribute.attributeTypeValue">',
    '                        <!--展示材料属性选项-->',
    '                        <el-select style="width: 130px"',
    '                                   v-model="material[quoteParts.constructId].defaultOption"',
    '                                   @change="materialChange($event,quoteParts.constructId,quoteAttribute.quoteAttributeOptionList)"',
    '                                   clearable placeholder="材料">',
    '                            <el-option',
    '                                    v-for="quoteOption in quoteAttribute.quoteAttributeOptionList"',
    '                                    :key="quoteOption.optionKey"',
    '                                    :label="quoteOption.name"',
    '                                    :value="quoteOption.optionKey">',
    '                            </el-option>',
    '                        </el-select>',
    '                        <el-select',
    '                                style="width: 100px"',
    '                                v-model="calculateData.attributesMap[quoteParts.constructId][quoteAttribute.attributeTypeValue]"',
    '                                clearable placeholder="克重">',
    '                            <el-option',
    '                                    v-for="materialOption in material[quoteParts.constructId].sonOptionList"',
    '                                    :key="materialOption.id"',
    '                                    :label="materialOption.name"',
    '                                    :value="materialOption.id">',
    '                            </el-option>',
    '                        </el-select>',
    '                        <el-checkbox  v-show="false"' ,
    '                                v-if="quoteParts.selfPaper === true"',
    '                                v-model="calculateData.selfPaperParts[quoteParts.constructId]" style="font-size: 12px">',
    '                            <span style="font-size: 10px">自来纸</span>',
    '                        </el-checkbox>',
    '                    </el-form-item>',
    '                    <!--非必选显示工艺-->',
    '                    <p v-show="isShowCraft[quoteParts.constructId]">',
    '                        <el-checkbox v-model="addShowCraft[quoteParts.constructId]" @change="changeCraft(quoteParts.constructId)">添加工艺</el-checkbox>',
    '                    </p>',
    '                    <el-form-item',
    '                            style="margin-bottom: 0px"',
    '                            v-for="quoteAttribute in quoteParts.quoteAttributeList"',
    '                            v-if="quoteAttribute.attributeType == \'craft\' && quoteAttribute.mustChioce != true &&',
    '                            quoteAttribute.attributeDisplay == true && addShowCraft[quoteParts.constructId] == true "',
    '                            :label="quoteAttribute.name"',
    '                            :prop="quoteAttribute.attributeTypeValue">',
    '                        <!--展示属性选项-->',
    '                        <el-select',
    '                                style="width: 130px"',
    '                                v-model="calculateData.attributesMap[quoteParts.constructId][quoteAttribute.attributeTypeValue]"',
    '                                clearable placeholder="请选择">',
    '                            <el-option',
    '                                    v-for="quoteOption in quoteAttribute.quoteAttributeOptionList"',
    '                                    :key="quoteOption.id"',
    '                                    :label="quoteOption.name"',
    '                                    :value="quoteOption.id">',
    '                            </el-option>',
    '                        </el-select>',
    '                    </el-form-item>',
    '                </el-collapse-item>',
    '                   <el-button v-show="ziyunCategoryId!=null"' ,
    '                           style="margin: 5px;margin-left: 40%;"',
    '                           size="mini"',
    '                           v-if="mustChoiceParts.length >0"',
    '                           type="primary"',
    '                           @click="submitForm(\'calculateForm\')">算价',
    '                   </el-button>',
    '            </el-form>',
    '        </el-collapse>',
    '           </el-tab-pane>',
    '       <el-tab-pane label="商务印刷" name="print" @tab-click="changeGoodsTab">',
    '                   <el-tabs v-model="ziyunCategoryId" type="card" @tab-click="handlePrintClick">',
    '                   <el-tab-pane label="快印商品"',
    '                     v-for="(item, index) in print"',
    '                      :key="item.categoryId"',
    '                   :label="item.categoryName"',
    '                     :name="item.ziyunCategoryId">',
    '                    </el-tab-pane>',
    '                       </el-tabs>',
    '        <!--产品信息展示-->',
    '        <!--1. 循环部件-->',
    '        <el-collapse v-model="mustChoiceParts" style="padding-left:15px">',
    '            <el-form :inline="true" :model="calculateData" :rules="calculateFormRules"  ref="calculateForm"',
    '                     label-width="100px" class="demo-form-inline" size="mini">',
    '                <el-collapse-item ',
    '                        v-for="quoteParts in quoteProductInfo.quotePartsList"',
    '                        :title="quoteParts.name"',
    '                        :name="quoteParts.constructId"',
    '                >',
    '                    <!--展示每个部件quoteAttributeList-->',
    '                    <!--普通属性-->',
    '                    <el-form-item',
    '                            style="margin-bottom: 0px"',
    '                            v-for="quoteAttribute in quoteParts.quoteAttributeList"',
    '                            v-if="quoteAttribute.attributeType == \'normal\' "',
    '                            >',
    '                        <el-form-item',
    '                                style="margin-bottom: 0px"',
    '                                :label="quoteAttribute.name"',
    '                                :prop="\'attributesMap.\'+quoteParts.constructId+\'.\'+quoteAttribute.attributeTypeValue"',
    '                        >',
    '                            <!--展示属性选项-->',
    '                            <el-select',
    '                                    style="width: 130px"',
    '                                    v-model="calculateData.attributesMap[quoteParts.constructId][quoteAttribute.attributeTypeValue]"',
    '                                    clearable placeholder="请选择" >',
    '                                <el-option',
    '                                        v-for="quoteOption in quoteAttribute.quoteAttributeOptionList"',
    '                                        :key="quoteOption.id"',
    '                                        :label="quoteOption.name"',
    '                                        :value="quoteOption.id">',
    '                                </el-option>',
    '                            </el-select>',
    '                        </el-form-item>',
    '                        <!--展示自定义-->',
    '                        <el-checkbox v-model="isShowDefined[quoteParts.constructId][quoteAttribute.attributeTypeValue]"',
    '                                     v-if="quoteAttribute.canDefinition"',
    '                                     @change="defineChange(quoteParts.constructId,quoteAttribute,$event)">',
    '                            自定义',
    '                        </el-checkbox>',
    '                        <el-form-item',
    '                                v-if="isShowDefined[quoteParts.constructId][quoteAttribute.attributeTypeValue]"',
    '                                v-for="(define,index) in definedArr[quoteParts.constructId][quoteAttribute.attributeTypeValue]"',
    '                                :prop="\'definitionValueMap.\'+quoteParts.constructId+\'.\'+quoteAttribute.attributeTypeValue+\'.\'+index">',
    '                            <el-input v-model.number="calculateData.definitionValueMap[quoteParts.constructId][quoteAttribute.attributeTypeValue][index]"',
    '                                      style="width: 70px"',
    '                                      :placeholder="define">',
    '                            </el-input>',
    '                        </el-form-item>',
    '                    </el-form-item>',
    '                    <!--必选显示工艺-->',
    '                    <el-form-item',
    '                            style="margin-bottom: 0px"',
    '                            v-for="quoteAttribute in quoteParts.quoteAttributeList"',
    '                            v-if="quoteAttribute.attributeType == \'craft\' && quoteAttribute.mustChioce == true &&',
    '                            quoteAttribute.attributeDisplay == true "',
    '                            :label="quoteAttribute.name"',
    '                            :prop="\'attributesMap.\'+quoteParts.constructId+\'.\'+quoteAttribute.attributeTypeValue">',
    '                        <!--展示属性选项-->',
    '                        <el-select',
    '                                style="width: 130px"',
    '                                v-model="calculateData.attributesMap[quoteParts.constructId][quoteAttribute.attributeTypeValue]"',
    '                                clearable placeholder="请选择">',
    '                            <el-option',
    '                                    v-for="quoteOption in quoteAttribute.quoteAttributeOptionList"',
    '                                    :key="quoteOption.id"',
    '                                    :label="quoteOption.name"',
    '                                    :value="quoteOption.id">',
    '                            </el-option>',
    '                        </el-select>',
    '                    </el-form-item>',
    '                    <!--材料-->',
    '                    <el-form-item',
    '                            style="margin-bottom: 0px"',
    '                            v-for="quoteAttribute in quoteParts.quoteAttributeList"',
    '                            v-if="quoteAttribute.attributeType == \'material\' "',
    '                            :label="quoteAttribute.name"',
    '                            :prop="quoteParts.constructId+quoteAttribute.attributeTypeValue">',
    '                        <!--展示材料属性选项-->',
    '                        <el-select style="width: 130px"',
    '                                   v-model="material[quoteParts.constructId].defaultOption"',
    '                                   @change="materialChange($event,quoteParts.constructId,quoteAttribute.quoteAttributeOptionList)"',
    '                                   clearable placeholder="材料">',
    '                            <el-option',
    '                                    v-for="quoteOption in quoteAttribute.quoteAttributeOptionList"',
    '                                    :key="quoteOption.optionKey"',
    '                                    :label="quoteOption.name"',
    '                                    :value="quoteOption.optionKey">',
    '                            </el-option>',
    '                        </el-select>',
    '                        <el-select',
    '                                style="width: 100px"',
    '                                v-model="calculateData.attributesMap[quoteParts.constructId][quoteAttribute.attributeTypeValue]"',
    '                                clearable placeholder="克重">',
    '                            <el-option',
    '                                    v-for="materialOption in material[quoteParts.constructId].sonOptionList"',
    '                                    :key="materialOption.id"',
    '                                    :label="materialOption.name"',
    '                                    :value="materialOption.id">',
    '                            </el-option>',
    '                        </el-select>',
    '                        <el-checkbox',
    '                                v-if="quoteParts.selfPaper === true"',
    '                                v-model="calculateData.selfPaperParts[quoteParts.constructId]" style="font-size: 12px">',
    '                            <span style="font-size: 10px">自来纸</span>',
    '                        </el-checkbox>',
    '                    </el-form-item>',
    '                    <!--非必选显示工艺-->',
    '                    <p v-show="isShowCraft[quoteParts.constructId]">',
    '                        <el-checkbox v-model="addShowCraft[quoteParts.constructId]" @change="changeCraft">添加工艺</el-checkbox>',
    '                    </p>',
    '                    <el-form-item',
    '                            style="margin-bottom: 0px"',
    '                            v-for="quoteAttribute in quoteParts.quoteAttributeList"',
    '                            v-if="quoteAttribute.attributeType == \'craft\' && quoteAttribute.mustChioce != true &&',
    '                            quoteAttribute.attributeDisplay == true && addShowCraft[quoteParts.constructId] == true "',
    '                            :label="quoteAttribute.name"',
    '                            :prop="quoteAttribute.attributeTypeValue">',
    '                        <!--展示属性选项-->',
    '                        <el-select',
    '                                style="width: 130px"',
    '                                v-model="calculateData.attributesMap[quoteParts.constructId][quoteAttribute.attributeTypeValue]"',
    '                                clearable placeholder="请选择">',
    '                            <el-option',
    '                                    v-for="quoteOption in quoteAttribute.quoteAttributeOptionList"',
    '                                    :key="quoteOption.id"',
    '                                    :label="quoteOption.name"',
    '                                    :value="quoteOption.id">',
    '                            </el-option>',
    '                        </el-select>',
    '                    </el-form-item>',
    '                </el-collapse-item>',
    '                   <el-button v-show="ziyunCategoryId!=null"',
    '                           style="margin: 5px;margin-left: 40%;"',
    '                           size="mini"',
    '                           v-if="mustChoiceParts.length >0"',
    '                           type="primary"',
    '                           @click="submitForm(\'calculateForm\')">算价',
    '                   </el-button>',
    '            </el-form>',
    '        </el-collapse>',
    '           </el-tab-pane>',
    '   <el-tab-pane label="标品" name="standard">',
    '     </el-tab-pane>',
    '        </el-tabs>',
    '        <!--标品组件-->',
    '        <el-dialog',
    '               ref="standPriceDialog" ' ,
    '                title="标品"',
    '                top="5"',
    '                :visible.sync="visible.showStandDialog"',
    '                fullscreen="true"',
    '               append-to-body>',
    '            <zy-calculate-stand-price ' ,
    '               ref="calculateStandPrice" ' ,
    '            @zy-add-stand-goods="zyAddStandGoods" ',
    '           @zy-cancel-stand-goods="zyCancelStandGoods" >',
    '            </zy-calculate-stand-price>',
    '        </el-dialog>',
    '        <!--计算价格展示-->',
    '        <el-dialog',
    '                title="销售价格" v-dialogDrag',
    '                :visible.sync="visible.quotePriceVisible"',
    '                width="550px" @close-on-click-modal="false"' ,
    '                center append-to-body>',
    '            <template>',
    '                <div style="text-align: center">',
    '                    <el-radio style="height: auto" v-for="(fastFactoryQuoteInfo, index) in quotePriceInfoList" border',
    '                              v-model="selectedPrice" :label="fastFactoryQuoteInfo">',
    '                        <div>{{fastFactoryQuoteInfo.quantity}}本</div>',
    '                        <div style="margin-top: 5px">{{fastFactoryQuoteInfo.unitPrice}}/本</div>',
    '                        <div style="margin-top: 5px">{{fastFactoryQuoteInfo.totalPrice}}元</div>',
    '                    </el-radio>',
    '                </div>',
    '                <div style="margin-top: 30px ;text-align: center"> 销售金额：',
    '                    <el-input placeholder="输入销售金额" v-model="salesAmount" style="width:180px" clearable',
    '                              size="small"></el-input>',
    '                </div>',
    '            </template>',
    '            <span slot="footer" class="dialog-footer">',
    '    <el-button type="primary" @click="addCalculatePrice" size="small" >确 定</el-button>',
    '  </span>',
    '        </el-dialog>',
    '      ',
    '    </div>'
].join("");

Vue.component('zy-calculate-price', {
    template:htmlCodes,
    props: {
        fast: {},
        print: {},
        isEdit: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            visible:{
                showStandDialog: false,
                quotePriceVisible: false,
            },
            quoteProductInfo: {}, //报价产品信息
            category:{},// 分类类型
            mustChoiceParts: [],//必选部件
            calculateData: {memberId:99999999},// 算价数据
            calculateFormRules: {}, // 算价表单规则
            material: {},//材料属性临时存放
            isShowCraft: {},// 是否显示非必选工艺
            addShowCraft: {},// 是否显示添加工艺
            isShowDefined: {}, //是否显示自定义
            definedArr: {}, //自定义数组
            quotePriceInfoList: [],// 算价结果
            selectedPrice: {},// 选中的算价结果
            salesAmount: 0,// 销售金额
            goodsData: [],// 标品商品数据
            memberId:99999999,
            ziyunCategoryId:null //分类id
        }
    },
    methods: {
        //获取会员id
        getMemberId:function(memberId){
            if(!isBlank(memberId)){
                this.memberId=memberId;
                this.calculateData.memberId=memberId;
            }

        },
        //菜单切换
        handleSelect(key, keyPath) {
            if (key === '3'){ // 标品打开新弹窗
                this.visible.showStandDialog = true;
                setTimeout(() =>
                    this.$refs.calculateStandPrice.getStandardMemberId(this.memberId));
                    return;
            }
            //发起请求获取报价模板
            var self = this;
            $.get(baseURL + "order/erporder/getQuoteGoods/" + key, function (r) {
                // 获取菜单类型
                self.category = self.$refs.categoryMenu.items[key].$attrs.type;
                //初始化数据
                self.calculateFormRules = {};
                self.calculateData = {
                    categoryType: '',         //分类
                    attributesMap: {},       // 工艺属性
                    selfPaperParts: {},      // 自来纸
                    definitionValueMap: {},  // 自定义
                    categoryId: key,
                    memberId:self.memberId
                }
                if (r.code == 0) {
                    self.quoteProductInfo = r.quoteProductInfo;
                    self.calculateData.categoryType = r.quoteProductInfo.categoryType;
                    // 解析数据
                    for (var i = 0; i < self.quoteProductInfo.quotePartsList.length; i++) {
                        var temp = self.quoteProductInfo.quotePartsList[i];
                        //取出必选部件
                        if (temp.mustChoice == true) {
                            self.mustChoiceParts.push(temp.constructId);
                        }
                        //赋值初始值
                        //提交表单属性初始化
                        Vue.set(self.calculateData.attributesMap, temp.constructId, {});
                        //提交表单自定义值初始化
                        Vue.set(self.calculateData.definitionValueMap, temp.constructId, {});
                        //提交表单自来纸初始化
                        Vue.set( self.calculateData.selfPaperParts, temp.constructId, false);
                        //临时材料初始化
                        Vue.set(self.material, temp.constructId, {});
                        //临时 是否展示自定义 初始化
                        Vue.set(self.isShowDefined, temp.constructId, {});
                        //临时 自定义数组 初始化
                        Vue.set(self.definedArr, temp.constructId, {});
                        //临时 是否展示自定义按钮初始化
                        Vue.set(self.addShowCraft, temp.constructId, false);
                        //赋值默认选项
                        //循环 quoteAttributeOptionList
                        for (var quoteAttribute of temp.quoteAttributeList) {
                            if (quoteAttribute.canDefinition === true) {
                                Vue.set(self.calculateData.definitionValueMap[temp.constructId], quoteAttribute.attributeTypeValue, {});
                            }

                            for (var quoteOption of quoteAttribute.quoteAttributeOptionList) {
                                // 普通属性默认值
                                if (quoteAttribute.attributeType === 'normal' && quoteOption.defaultOption === true) {
                                    Vue.set(self.calculateData.attributesMap[quoteAttribute.constructId], quoteAttribute.attributeTypeValue, quoteOption.id);
                                    //break loopOption;
                                }
                                // 必选工艺 赋默认值
                                if (quoteAttribute.attributeType === 'craft' && quoteAttribute.mustChioce === true && quoteOption.defaultOption === true) {
                                    Vue.set(self.calculateData.attributesMap[quoteAttribute.constructId], quoteAttribute.attributeTypeValue, quoteOption.id);
                                }
                                // 非必选工艺
                                if (quoteAttribute.attributeType === 'craft' && quoteAttribute.mustChioce != true) {
                                    Vue.set(self.isShowCraft, temp.constructId, true);
                                }

                                // 材料特殊处理  寻找克重
                                if (quoteAttribute.attributeType === 'material' && quoteOption.defaultOption === true) {
                                    // 先赋值材料
                                    Vue.set(self.material[quoteAttribute.constructId], 'defaultOption', quoteOption.optionKey);
                                    Vue.set(self.material[quoteAttribute.constructId], 'sonOptionList', quoteOption.sonOptionList);
                                    // 循环当前克重 找出默认克重
                                    loopSonOption:
                                        for (var sonOption of quoteOption.sonOptionList) {
                                            if (sonOption.defaultOption === true) {
                                                Vue.set(self.calculateData.attributesMap[quoteAttribute.constructId], quoteAttribute.attributeTypeValue, sonOption.id);
                                                break loopSonOption;
                                            }
                                        }
                                }

                            }
                            //生成表单校验规则
                            if (quoteAttribute.mustChioce === true) {
                                var rule = {required: true,  message: '请选择' + quoteAttribute.name, trigger: 'change'};
                                Vue.set(self.calculateFormRules, 'attributesMap.'+ temp.constructId +'.'+ quoteAttribute.attributeTypeValue, rule);
                            }
                        }
                    }
                } else {
                    alert(r.msg);
                }
            });
        },

        changeCraft:function(value){
            var self=this;
            for (var i = 0; i < self.quoteProductInfo.quotePartsList.length; i++) {
                var quoteParts=self.quoteProductInfo.quotePartsList[i];
                for (var j= 0;j <quoteParts.quoteAttributeList.length;j++){
                    var quoteAttribute=quoteParts.quoteAttributeList[j];
                    if(quoteAttribute.attributeType == 'craft' && quoteAttribute.mustChioce !=true){
                         for (var key in self.calculateData.attributesMap[value]){
                             if(key==quoteAttribute.attributeTypeValue){
                                 delete  self.calculateData.attributesMap[value][key];
                             }
                         }
                    }
                }
            }
        },

        /* 材料切换,克重置为空 */
        materialChange: function (optionKey, constructId, optionList) {
            //重置 克重下拉框option
            var option = optionList.filter(function (option) {
                return option.optionKey === optionKey;
            });
            if (option.length == 1) {
                this.material[constructId].sonOptionList = option[0].sonOptionList;
            } else if (option.length == 0) {
                this.material[constructId].sonOptionList = null;
            }
            // 清空克重
            var material = this.calculateData.attributesMap[constructId].material;
            if (material) {
                this.calculateData.attributesMap[constructId].material = null;
            }
        },

        /*自定义切换*/
        defineChange: function (constructId, quoteAttribute, value) {
            if(value == false){ //取消自定义
                //清除 自定义规则
                for(var i=0; i< this.definedArr[constructId][quoteAttribute.attributeTypeValue].length; i++){
                    Vue.set(this.calculateData.definitionValueMap[constructId][quoteAttribute.attributeTypeValue],i,null);
                    Vue.set(this.calculateFormRules, 'definitionValueMap.'+ constructId +'.'+ quoteAttribute.attributeTypeValue+'.'+i, null);
                }
                Vue.delete( this.calculateData.definitionValueMap[constructId], quoteAttribute.attributeTypeValue );
                Vue.delete( this.definedArr[constructId], quoteAttribute.attributeTypeValue );
                return;
            }
            //先查找当前选项id
            var optionId = this.calculateData.attributesMap[constructId][quoteAttribute.attributeTypeValue];
            // 查找当前选项
            var option = $.grep(quoteAttribute.quoteAttributeOptionList, function (temp) {
                return temp.id === optionId;
            });
            if (option.length == 0) {
                this.$message({showClose: true, message: '请先选择选项 !', type: 'warning'});
                return;
            }
            if (option.length > 1) {
                this.$message({showClose: true, message: '选项数据异常，请联系管理员 !', type: 'error'});
                return;
            }
            //判断当前选项是否有 自定义
            var arrTemp = [];
            if (option.canDefinition === true) {
                // 解析自定义选项
                arrTemp = JSON.parse(option.definitionLength);

            } else {
                // 取属性自定义
                if (quoteAttribute.canDefinition === true) {
                    arrTemp = JSON.parse(quoteAttribute.definitionLength);
                }
            }

            if(arrTemp.length >0){
                // 赋值自定义数据
                Vue.set(this.definedArr[constructId], quoteAttribute.attributeTypeValue, arrTemp);

                // 动态绑定数组值
                Vue.set(this.calculateData.definitionValueMap[constructId],quoteAttribute.attributeTypeValue,{});
                for (var i =0 ;i < arrTemp.length;i ++){
                    Vue.set(this.calculateData.definitionValueMap[constructId][quoteAttribute.attributeTypeValue],i,null);
                    // 赋值表单校验
                    var rule = {required: true,type: 'number',  min: arrTemp[i][0], max: arrTemp[i][1], message: arrTemp[i][0]+'-'+arrTemp[i][1]+' ', trigger: 'blur' }
                    Vue.set(this.calculateFormRules, 'definitionValueMap.'+ constructId +'.'+ quoteAttribute.attributeTypeValue+'.'+i, rule);
                }
            }
        },

        /*提交表单计算价格*/
        submitForm: function (formName) {
            var self = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    var  url = '';
                    if (self.category === 'fast'){
                        url = "order/erporder/fastCaculateQuote";
                    }else if(self.category === 'print'){
                        url = "order/erporder/caculateQuote";
                    }else {
                        alert("数据异常,请刷新页面!");
                    }
                    const loading = self.$loading({
                        lock: true,
                        text: '算价中,请勿刷新页面',
                        spinner: 'el-icon-loading'
                    });

                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(self.calculateData),
                        success: function(r){
                            loading.close();
                            if (r.code == 0) {
                                if (self.category === 'fast') {
                                    self.quotePriceInfoList = r.fastFactoryQuoteInfoList;
                                    //默认第一个选中
                                    self.selectedPrice = r.fastFactoryQuoteInfoList[0];
                                    self.visible.quotePriceVisible = true;
                                } else if (self.category === 'print') {
                                    self.quotePriceInfoList = r.factoryQuoteInfoList;
                                    // 默认第三个选中
                                    self.selectedPrice = r.factoryQuoteInfoList[2];
                                    self.visible.quotePriceVisible = true;
                                }
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                } else {
                    this.$message.error('请按页面提示修改参数!');
                    return false;
                }
            });

        },

        /*添加选中价格到订单详情*/
        addCalculatePrice: function () {
            var goods = {};
            goods.productId = this.selectedPrice.productId;
            goods.productName = this.selectedPrice.productName;
            goods.quantity = this.selectedPrice.quantity;
            goods.price = this.selectedPrice.unitPrice;
            goods.total = this.selectedPrice.totalPrice;
            goods.goodsType = this.category; // 类型
            goods.craft = this.selectedPrice.addon;
            goods.calculationPriceProcess = this.selectedPrice.priceProcess;
            goods.productInfoParamList = this.selectedPrice.productInfoParamList;
            goods.attributeOption = this.selectedPrice.attributeOption;
            goods.quoteSelfDefineParam = this.selectedPrice.quoteSelfDefineParam;
            goods.cutModel = this.selectedPrice.cutModel;
            goods.showAddon=this.selectedPrice.showAddon;
            goods.cutModelObj = JSON.parse(goods.cutModel);
            goods.goodsId=this.selectedPrice.goodsId;
            goods.copy=1;
            //如果输入了金额，对总价重新计算
            if (this.salesAmount != 0) {
                goods.itemRemark="商品原价为:"+goods.total+";修改后价格为:"+this.salesAmount;
                goods.total = this.salesAmount;
                goods.price = (goods.total / goods.quantity).toFixed(3);
            }else{
                goods.itemRemark="";
            }
            this.visible.quotePriceVisible = false;
            //清空选择和输入的数据
            this.salesAmount = 0;
            this.selectedPrice = {};
            // 报价商品事件抛出 goods 转成数据 与标品保持数据格式一致
            var temp = [];
            temp.push(goods);
            this.$emit('zy-add-goods', temp,goods.goodsType);
        },

        /*标品添加事件*/
        zyAddStandGoods: function (data) {
            // 标品事件抛出
            this.$emit('zy-add-goods', data,"standard");
            this.visible.showStandDialog = false;
        },

        /*取消添加 事件*/
        zyCancelStandGoods: function () {
            this.visible.showStandDialog = false;

        },
        //修改一级tab
        changeGoodsTab:function () {
            this.quoteProductInfo= {};
            this.ziyunCategoryId=null;
            if (this.category === 'standard'){ // 标品打开新弹窗
                this.visible.showStandDialog = true;
                setTimeout(() =>
                    this.$refs.calculateStandPrice.getStandardMemberId(this.memberId));
                return;
            }
        },
        handleFastClick:function () {
        //发起请求获取报价模板
            var self = this;
            self.category="fast";
            const loading = self.$loading({
                lock: true,
                text: '请求报价参数中，请勿刷新页面',
                spinner: 'el-icon-loading'
            });
            $.get(baseURL + "order/erporder/getQuoteGoods/" + self.ziyunCategoryId, function (r) {
                loading.close();
                //初始化数据
                self.calculateFormRules = {};
                self.calculateData = {
                    categoryType: '',         //分类
                    attributesMap: {},       // 工艺属性
                    selfPaperParts: {},      // 自来纸
                    definitionValueMap: {},  // 自定义
                    categoryId: self.ziyunCategoryId,
                    memberId:self.memberId
                }
                if (r.code == 0) {
                    self.quoteProductInfo = r.quoteProductInfo;
                    self.calculateData.categoryType = r.quoteProductInfo.categoryType;
                    // 解析数据
                    for (var i = 0; i < self.quoteProductInfo.quotePartsList.length; i++) {
                        var temp = self.quoteProductInfo.quotePartsList[i];
                        //取出必选部件
                        if (temp.mustChoice == true) {
                            self.mustChoiceParts.push(temp.constructId);
                        }
                        //赋值初始值
                        //提交表单属性初始化
                        Vue.set(self.calculateData.attributesMap, temp.constructId, {});
                        //提交表单自定义值初始化
                        Vue.set(self.calculateData.definitionValueMap, temp.constructId, {});
                        //提交表单自来纸初始化
                        Vue.set( self.calculateData.selfPaperParts, temp.constructId, false);
                        //临时材料初始化
                        Vue.set(self.material, temp.constructId, {});
                        //临时 是否展示自定义 初始化
                        Vue.set(self.isShowDefined, temp.constructId, {});
                        //临时 自定义数组 初始化
                        Vue.set(self.definedArr, temp.constructId, {});
                        //临时 是否展示自定义按钮初始化
                        Vue.set(self.addShowCraft, temp.constructId, false);
                        //赋值默认选项
                        //循环 quoteAttributeOptionList
                        for (var quoteAttribute of temp.quoteAttributeList) {
                            if (quoteAttribute.canDefinition === true) {
                                Vue.set(self.calculateData.definitionValueMap[temp.constructId], quoteAttribute.attributeTypeValue, {});
                            }

                            for (var quoteOption of quoteAttribute.quoteAttributeOptionList) {
                                // 普通属性默认值
                                if (quoteAttribute.attributeType === 'normal' && quoteOption.defaultOption === true) {
                                    Vue.set(self.calculateData.attributesMap[quoteAttribute.constructId], quoteAttribute.attributeTypeValue, quoteOption.id);
                                    //break loopOption;
                                }
                                // 必选工艺 赋默认值
                                if (quoteAttribute.attributeType === 'craft' && quoteAttribute.mustChioce === true && quoteOption.defaultOption === true) {
                                    Vue.set(self.calculateData.attributesMap[quoteAttribute.constructId], quoteAttribute.attributeTypeValue, quoteOption.id);
                                }
                                // 非必选工艺
                                if (quoteAttribute.attributeType === 'craft' && quoteAttribute.mustChioce != true) {
                                    Vue.set(self.isShowCraft, temp.constructId, true);
                                }

                                // 材料特殊处理  寻找克重
                                if (quoteAttribute.attributeType === 'material' && quoteOption.defaultOption === true) {
                                    // 先赋值材料
                                    Vue.set(self.material[quoteAttribute.constructId], 'defaultOption', quoteOption.optionKey);
                                    Vue.set(self.material[quoteAttribute.constructId], 'sonOptionList', quoteOption.sonOptionList);
                                    // 循环当前克重 找出默认克重
                                    loopSonOption:
                                        for (var sonOption of quoteOption.sonOptionList) {
                                            if (sonOption.defaultOption === true) {
                                                Vue.set(self.calculateData.attributesMap[quoteAttribute.constructId], quoteAttribute.attributeTypeValue, sonOption.id);
                                                break loopSonOption;
                                            }
                                        }
                                }

                            }
                            //生成表单校验规则
                            if (quoteAttribute.mustChioce === true) {
                                var rule = {required: true,  message: '请选择' + quoteAttribute.name, trigger: 'change'};
                                Vue.set(self.calculateFormRules, 'attributesMap.'+ temp.constructId +'.'+ quoteAttribute.attributeTypeValue, rule);
                            }
                        }
                    }
                } else {
                    alert(r.msg);
                    loading.close();
                }
            });
        },

        handlePrintClick:function () {
        //发起请求获取报价模板
        var self = this;
        self.category="print";
        const loading = self.$loading({
            lock: true,
            text: '请求报价参数中，请勿刷新页面',
            spinner: 'el-icon-loading'
        });
        $.get(baseURL + "order/erporder/getQuoteGoods/" + self.ziyunCategoryId, function (r) {
            loading.close();
            //初始化数据
            self.calculateFormRules = {};
            self.calculateData = {
                categoryType: '',         //分类
                attributesMap: {},       // 工艺属性
                selfPaperParts: {},      // 自来纸
                definitionValueMap: {},  // 自定义
                categoryId: self.ziyunCategoryId,
                memberId:self.memberId
            }
            if (r.code == 0) {
                self.quoteProductInfo = r.quoteProductInfo;
                self.calculateData.categoryType = r.quoteProductInfo.categoryType;
                // 解析数据
                for (var i = 0; i < self.quoteProductInfo.quotePartsList.length; i++) {
                    var temp = self.quoteProductInfo.quotePartsList[i];
                    //取出必选部件
                    if (temp.mustChoice == true) {
                        self.mustChoiceParts.push(temp.constructId);
                    }
                    //赋值初始值
                    //提交表单属性初始化
                    Vue.set(self.calculateData.attributesMap, temp.constructId, {});
                    //提交表单自定义值初始化
                    Vue.set(self.calculateData.definitionValueMap, temp.constructId, {});
                    //提交表单自来纸初始化
                    Vue.set( self.calculateData.selfPaperParts, temp.constructId, false);
                    //临时材料初始化
                    Vue.set(self.material, temp.constructId, {});
                    //临时 是否展示自定义 初始化
                    Vue.set(self.isShowDefined, temp.constructId, {});
                    //临时 自定义数组 初始化
                    Vue.set(self.definedArr, temp.constructId, {});
                    //临时 是否展示自定义按钮初始化
                    Vue.set(self.addShowCraft, temp.constructId, false);
                    //赋值默认选项
                    //循环 quoteAttributeOptionList
                    for (var quoteAttribute of temp.quoteAttributeList) {
                        if (quoteAttribute.canDefinition === true) {
                            Vue.set(self.calculateData.definitionValueMap[temp.constructId], quoteAttribute.attributeTypeValue, {});
                        }

                        for (var quoteOption of quoteAttribute.quoteAttributeOptionList) {
                            // 普通属性默认值
                            if (quoteAttribute.attributeType === 'normal' && quoteOption.defaultOption === true) {
                                Vue.set(self.calculateData.attributesMap[quoteAttribute.constructId], quoteAttribute.attributeTypeValue, quoteOption.id);
                                //break loopOption;
                            }
                            // 必选工艺 赋默认值
                            if (quoteAttribute.attributeType === 'craft' && quoteAttribute.mustChioce === true && quoteOption.defaultOption === true) {
                                Vue.set(self.calculateData.attributesMap[quoteAttribute.constructId], quoteAttribute.attributeTypeValue, quoteOption.id);
                            }
                            // 非必选工艺
                            if (quoteAttribute.attributeType === 'craft' && quoteAttribute.mustChioce != true) {
                                Vue.set(self.isShowCraft, temp.constructId, true);
                            }

                            // 材料特殊处理  寻找克重
                            if (quoteAttribute.attributeType === 'material' && quoteOption.defaultOption === true) {
                                // 先赋值材料
                                Vue.set(self.material[quoteAttribute.constructId], 'defaultOption', quoteOption.optionKey);
                                Vue.set(self.material[quoteAttribute.constructId], 'sonOptionList', quoteOption.sonOptionList);
                                // 循环当前克重 找出默认克重
                                loopSonOption:
                                    for (var sonOption of quoteOption.sonOptionList) {
                                        if (sonOption.defaultOption === true) {
                                            Vue.set(self.calculateData.attributesMap[quoteAttribute.constructId], quoteAttribute.attributeTypeValue, sonOption.id);
                                            break loopSonOption;
                                        }
                                    }
                            }

                        }
                        //生成表单校验规则
                        if (quoteAttribute.mustChioce === true) {
                            var rule = {required: true,  message: '请选择' + quoteAttribute.name, trigger: 'change'};
                            Vue.set(self.calculateFormRules, 'attributesMap.'+ temp.constructId +'.'+ quoteAttribute.attributeTypeValue, rule);
                        }
                    }
                }
            } else {
                alert(r.msg);
                loading.close();
            }
        });
    }
        },
});

